package com.example.phone.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.phone.R;
import com.example.phone.bean.Product;
import com.loopj.android.image.SmartImageView;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2018/12/26 0026.
 */

public class GridViewAdapter extends BaseAdapter {
    Context context;
    List<Product> list;
    public static final String path = "http://120.77.253.51:8080/PhoneShop/Images/";

    public GridViewAdapter(Context _context, List<Product> _list) {
        this.list = _list;
        this.context = _context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get( position );
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from( context );
        convertView = layoutInflater.inflate( R.layout.list_item, null );
        SmartImageView shopImg = (SmartImageView) convertView.findViewById( R.id.ItemImage );
        TextView tv_name = (TextView) convertView.findViewById( R.id.tv_name );
        TextView tv_price = (TextView) convertView.findViewById( R.id.tv_price );
        Product shop = list.get( position );
        shopImg.setImageUrl(path + shop.getMainImage());
        tv_name.setText( shop.getName() );
        tv_price.setText( shop.getPrice().setScale(1, BigDecimal.ROUND_HALF_UP) + "" );
        return convertView;
    }
}
